<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="订单号">
                <a-input allow-clear placeholder="请输入订单号" v-model="queryParam.order_no"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="起止时间">
                <a-range-picker
                  style="width:100%"
                  format="YYYY-MM-DD HH:mm"
                  valueFormat="YYYY-MM-DD HH:mm"
                  :placeholder="['开始时间', '结束时间']"
                  v-model="queryTime"
                  @change="changeTime"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24" v-if="salesmanList.length">
              <a-form-item label="业务员">
                <a-select v-model="queryParam.salesman_id" allowClear>
                  <a-select-option value="">全部</a-select-option>
                  <a-select-option
                    :value="item.id"
                    v-for="item of salesmanList"
                    :key="item.id">{{item.name}}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getList">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="0"> 待处理 </a-radio-button>
          <a-radio-button value="1"> 已完成 </a-radio-button>
          <a-radio-button value="2"> 已拒绝 </a-radio-button>
        </a-radio-group>
        <div>
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="allDelete">批量删除</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
        :loading="btnLoading"
      >
        <span slot="mobile" slot-scope="scope">
          <a-tooltip>
            <template slot="title">
              <div class="addressInfo">
                <span>姓名:{{ scope.name }}</span>
                <span>手机:{{ scope.mobile }}</span>
                <span>地址:{{ scope.address }}</span>
              </div>
            </template>
            <div class="delivery_type">
              <span>{{ scope.name }}</span>
            </div>
          </a-tooltip>
        </span>
        <div slot="goods" slot-scope="scope">
          <div class="name" v-for="(item, index) in scope.goods_list" :key="index" style="margin-bottom: 10px">
            <div class="pic">
              <img :src="item.pic_url" alt="" />
            </div>
            <div class="info">
              <span>{{ item.name }}</span>
              <span>{{ item.attr_name }}</span>
            </div>
          </div>
        </div>

        <span slot="pay_time" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>

        <div slot="type" slot-scope="text">
          {{ text === '1' ? '退款退货' : '换货' }}
        </div>

        <div slot="refund_status" slot-scope="text" class="status">
          <span :style="text | orderStatus"></span>
          <span>{{ text | orderStatusFormate }}</span>
        </div>

        <div slot="action" slot-scope="scope">
          <a-button type="link" @click="see(scope)">查看</a-button>
          <a-button type="link" @click="printer(scope)">打印小票</a-button>

          <template v-if="parseInt(scope.refund_status) === -1">
            <a-divider type="vertical" />
            <a-button type="link" @click="changeStatus(scope.id, 'is_agree', 1)">同意</a-button>
            <a-divider type="vertical" />
            <a-button type="link" @click="changeStatus(scope.id, 'is_agree', 2)">拒绝</a-button>
          </template>

          <a-button type="link" @click="changeStatus(scope.id, 'status', 1)" v-if="parseInt(scope.refund_status) === 2"
            >确认收货</a-button
          >
        </div>
      </a-table>
    </a-card>

    <a-modal title="退/换货" :visible="showPop" @cancel="showPop = false" :width="800">
      <template slot="footer">
        <a-button key="submit" type="primary" :loading="btnLoading" @click="submitAdd"> 确定 </a-button>
      </template>

      <a-form-model
        ref="ruleForm"
        :rules="formRules"
        :model="form"
        :label-col="{ span: 3 }"
        :wrapper-col="{ span: 12 }"
      >
        <template v-if="form.is_agree === 1">
          <a-form-model-item label="收件人" prop="name">
            <a-input v-model="form.name" placeholder="请输入收件人" />
          </a-form-model-item>
          <a-form-model-item label="手机号" prop="mobile">
            <a-input v-model="form.mobile" placeholder="请输入手机号" />
          </a-form-model-item>
          <a-form-model-item label="详细地址" prop="detail">
            <a-input v-model="form.detail" placeholder="请输入详细地址" />
          </a-form-model-item>
          <a-form-model-item label="邮政编号" prop="zipcode">
            <a-input v-model="form.zipcode" placeholder="请输入邮政编号" />
          </a-form-model-item>
        </template>
        <a-form-model-item label="拒绝原因" prop="reason" v-else>
          <a-input v-model="form.reason" placeholder="请输入拒绝原因" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>

    <ContentDetails title="售后内容" ref="pop" @cancel="item = null">
      <div class="popContent" v-if="item">
        <p>{{ item.desc }}</p>

        <div class="img" v-if="item.pic_list">
          <img :src="e" alt="" v-for="(e, index) of item.pic_list" :key="e" @click="seeImg(e)" />
        </div>
        <video v-if="item.video" :src="item.video" controls></video>
      </div>
    </ContentDetails>
    <ModalImg :previewImage="previewImage" ref="ModalImg"></ModalImg>
  </page-header-wrapper>
</template>

<script>
import { transOrderRefundList, supplierApplyHandle } from '@/api/purchase/supplier'
import tabList from '@/views/saas/components/tabList.vue'
import { cyyUploadImg } from '@/components/CyyUi'
import {salesmanList} from "@/api/store"
import storage from 'store'
import { orderPrint } from '@/api/order'
import { supplierSendTypeRefundList } from '@/api/purchase/purchase'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id'
  },
  {
    title: '售后订单号',
    dataIndex: 'order_refund_no'
  },
  {
    title: '原订单号',
    dataIndex: 'old_order_no'
  },
  {
    title: '商品信息',
    scopedSlots: {
      customRender: 'goods'
    },
    width: 200
  },
  {
    title: '收货人',
    scopedSlots: {
      customRender: 'mobile'
    }
  },
  {
    title: '收货人电话',
    dataIndex: 'mobile',
    sorter: (a, b) => a.mobile - b.mobile
  },
  {
    title: '售后类型',
    dataIndex: 'type',
    scopedSlots: {
      customRender: 'type'
    }
  },
  {
    title: '退款金额',
    dataIndex: 'refund_price',
    sorter: (a, b) => a.refund_price - b.refund_price
  },
  {
    title: '售后状态',
    dataIndex: 'refund_status',
    scopedSlots: {
      customRender: 'refund_status'
    }
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action'
    }
  }
]

export default {
  components: {
    ContentDetails: () => import('@/components/tools/ContentDetail'),
    ModalImg: () => import('@/components/tools/ModalImg'),
    tabList,
    cyyUploadImg
  },
  data() {
    return {
      tabList: [
        {
          key: 'saasSupplierPurchaseAfterSaleOrder',
          name: '售后列表'
        }
      ],
      // 搜索参数
      queryParam: {
        page: 1,
        limit: 10,
        status: '-1',
        mch_id: '',
        start_time: '',
        end_time: '',
        order_no: '',
        order_type: 1,
        salesman_id:"",
        type: 0
      },
      salesmanList:[],
      columns,
      list: [],

      queryTime: [],

      showPop: false,
      form: {
        reason: '',
        // is_agree: 1,
        name: '',
        mobile: '',
        zipcode: ''
      },

      formRules: {
        // is_agree: [{ required: true, message: '请选择类型', trigger: 'change' }],
        reason: [{ required: true, message: '请输入拒绝原因', trigger: 'blur' }],
        name: [{ required: true, message: '请输入收件人', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        detail: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
        zipcode: [{ required: true, message: '请输入邮政编号', trigger: 'blur' }]
      },

      btnLoading: false,
      item: null,
      popInfo: false,
      previewImage: undefined,
      address: null
    }
  },

  watch: {
    showPop(e) {
      if (!e) {
        this.form = {
          reason: '',
          // is_agree: 1,
          name: '',
          mobile: '',
          zipcode: ''
        }
        this.$refs.ruleForm.resetFields()
      }
    }
  },

  filters: {
    orderStatusFormate(val) {
      if (val == -1) {
        return '等待供货商操作'
      }
      if (val == 1) {
        return '已同意待商家发货'
      }
      if (val == 2) {
        return '已同意待确认收货'
      }
      if (val == 3) {
        return '已同意已退款'
      }
      if (val == 4) {
        return '已经拒绝'
      }
      if (val == 5) {
        return '已同意已换货'
      }
    },

    orderStatus(val) {
      if (val == -1 || val == 0 || val === 4) {
        return 'background-color:#faad14;'
      }
      if (val == 1 || val == 2) {
        return 'background-color:#1890ff;'
      }
      if (val == 3 || val == 5) {
        return 'background-color:#65c817;'
      }
    }
  },
  created() {
    this.getList()
    let daili = storage.get('daili');
    if(daili) {
      salesmanList().then(res=>{
        this.salesmanList = res.data.list || []
      })
    };
  },
  methods: {
    printer(order) {
      const hide = this.$message.loading('提交中...', 0)
      orderPrint({
        order_id: order.order_id,
        type: 2,
        is_refund: 1
      }).then(res => {
        hide()
        if (res.code !== 0) {
          this.$error({
            content: res.msg
          })
        } else {
          this.$message.success(res.msg)
        }
      })
    },
    // 分页请求
    tablePageChange(e) {
      this.queryParam.page = e
      this.getList()
    },

    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      const res = await supplierSendTypeRefundList(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.list
        this.totalCount = res.data.count
        this.address = res.data.address
      }
      hide()
    },

    // 切换筛选时间
    changeTime(e) {
      if (e.length) {
        this.queryParam.start_time = e[0]
        this.queryParam.end_time = e[1]
      } else {
        this.queryParam.start_time = ''
        this.queryParam.end_time = ''
      }
    },

    changeStatus(order_id, type, value) {
      if (type === 'is_agree') {
        this.form.order_id = order_id
        this.form.is_agree = value
        this.showPop = true

        if (this.address) {
          let { name, mobile, detail, zipcode } = this.address
          this.form.name = name
          this.form.mobile = mobile
          this.form.detail = detail
          this.form.zipcode = zipcode
        }
        return
      }

      this.supplierApplyHandle({
        order_id,
        status: value
      })
    },

    async supplierApplyHandle(oldData) {
      this.btnLoading = true
      let { form: data } = this
      const res = await supplierApplyHandle(oldData || data)

      this.btnLoading = false
      if (res.code === 0) {
        this.$message.success('操作成功')
        this.getList()
        this.showPop = false
        this.item = null
      } else {
        this.$message.error(res.msg)
      }
    },

    submitAdd() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          let form = JSON.parse(JSON.stringify(this.form))

          this.supplierApplyHandle(form)
        } else {
          return false
        }
      })
    },

    see(item) {
      this.$refs.pop.visible = true
      this.item = item
    },

    seeImg(previewImage) {
      this.$refs.ModalImg.previewVisible = true
      this.previewImage = previewImage
    }
  }
}
</script>
<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    margin-top: 11px;
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
  .ant-btn {
    margin-top: 11px;
  }
}
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
.addressInfo {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: #fff;
}
.delivery_type {
  display: flex;
  flex-direction: column;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.85);
  margin: 5px 0;
}
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 4px;
  }
}

.popContent {
  .creatTime {
    padding-bottom: 10px;
  }

  .img {
    img {
      margin: 0 10px 10px 0;
      width: 100px;
      height: 100px;
      cursor: pointer;
    }
  }

  video {
    width: 100%;
    height: 400px;
  }

  h1 {
    font-size: 18px;
  }
}
</style>
